// pages/logIn/index.js
import {login,getTel,userRegisterByWx,getSessionKey } from '../../utils/api/index'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfor:'',
    sessionkey:'',
    encrypdata:'',
    ivdata:'',
    phone:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //获取用户信息 
  getPhoneNumber:function(e){
    const that=this
    that.setData({
      encrypdata:e.detail.encryptedData,
      ivdata:e.detail.iv
    })
    that.getUser()
  }, 
  //获取用户信息
  getUser:function(){
    const that=this
    wx.login({
      success: function(res) {
        if(res.code){
          var params={
            code:res.code
          }
          wx.showLoading({
            title: '加载中....',
                mask:true 
          })
          login(params).then(res=>{
            if(res.status==1){
              app.globalData.wxUserInfo=res.data
              app.globalData.userId=res.data.id
              app.globalData.openId=res.data.openId
              that.getSessionKey()
            }else if(res.status==0){
              app.globalData.openId=res.data
              that.getSessionKey()
            }else{
              wx.showToast({
                title:res.msg,
                icon: 'none',
                duration: 1500,
              })
            }
          })
        }else{
        }
      }
    });
  },

  //获取用户SessionKey
  getSessionKey:function(){
    var that=this
    wx.login({
      success: function(res) {
        if (res.code) {
          var params={
            code:res.code,
            openId:app.globalData.openId
          }
          getSessionKey(params).then(parameter=>{
            if(parameter.status==200){
              that.setData({
                  sessionkey:parameter.data
                })
                that.getTel()
            }
          })
        }
      }
    })
  },
  //获取用户手机号
  getTel:function(){
    var that=this
    var params={
      encrypdata:this.data.encrypdata,
      ivdata:this.data.ivdata,
      sessionkey:this.data.sessionkey
    }
    getTel(params).then(res=>{
      if(res.status==200){
        that.setData({
          phone:res.data
        })
        that.userRegisterByWx()
      }else{
        wx.showToast({
          title:'获取手机号失败',
          icon: 'none',
          duration: 1500,
        })
      }
    });
  },

  //登录注册
  userRegisterByWx:function(){
    const that=this
    var params={
      telPhone:that.data.phone,
      nickName:app.globalData.wxInfo.userInfo.nickName,
      headImg:app.globalData.wxInfo.userInfo.avatarUrl,
      openid:app.globalData.openId,
    }
    userRegisterByWx(params).then(res=>{
      if(res.status==200){
        app.globalData.wxUserInfo=res.data
        app.globalData.userId=res.data.id
        app.globalData.openId=res.data.openId
        wx.showToast({
          title:'登录成功',
          icon: 'none',
          duration: 1500,
          success:function(){
            setTimeout(function(){
              wx.navigateBack({
                delta: 1,
              })
            },1500)
          }
        })
      }else{
        if(res.msg=='该用户已被注册'){
          that.getLogin()
          return false
        }
        wx.showToast({
          title:res.msg,
          icon: 'none',
          duration: 1500,
        })
        
      }
      wx.hideLoading()
    })
  },
  //手机号登录
  otherLogin:function(){
    wx.navigateTo({
      url: '/pages/logIn/otherLogin/index'
    });
  },
  //登录
  getLogin:function(){
    wx.login({
      success: res => {


        wx.request({
          url: app.globalData.host+'/suser/authorization?code='+res.code,
          method: 'get',          
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: element => {
            if(element.data.status==1){
              if(element.data.data.phone!=null&&element.data.data.phone!=''){
                app.globalData.userId=element.data.data.id
                app.globalData.wxUserInfo=element.data.data
                app.globalData.openId=element.data.data.openId
                wx.showToast({
                  title:'登录成功',
                  icon: 'none',
                  duration: 1500,
                  success:function(){
                    setTimeout(function(){
                      wx.navigateBack({
                        delta: 1,
                      })
                    },1500)
                  }
                })
              }
            }else{
              app.globalData.openId=element.data.data
            }
          }
        })


      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})